/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Property;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;

/**
 * 
 */
public class PropertyQualifiersSection extends PropertyViewSection {


	/**
	 * 
	 */
	private Button isDerived, isDerivedUnion, isReadOnly, isStatic, isUnique, isOrdered;
	
	/**
	 * 
	 */
	private SelectionListener isDerivedListener, isDerivedUnionListener, isReadOnlyListener, isStaticListener, isUniqueListener, isOrderedListener;


	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {

		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory()
		.createFlatFormComposite(parent);
		FormData data;

		isDerived = getWidgetFactory().createButton(composite, "isDerived", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(0, 85);
		data.top = new FormAttachment(0, 0);
		isDerived.setLayoutData(data);

		isDerived.addSelectionListener(isDerivedListener = new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				((Property)getElement()).setIsDerived(isDerived.getSelection());
			}

			public void widgetDefaultSelected(SelectionEvent e){
			}
		});

		isDerivedUnion = getWidgetFactory().createButton(composite, "isDerivedUnion", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(isDerived, 20);
		data.top = new FormAttachment(0, 0);
		isDerivedUnion.setLayoutData(data);

		isDerivedUnion.addSelectionListener(isDerivedUnionListener = new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				((Property)getElement()).setIsDerivedUnion(isDerivedUnion.getSelection());
			}

			public void widgetDefaultSelected(SelectionEvent e){
			}
		});

		isOrdered = getWidgetFactory().createButton(composite, "isOrdered", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(isDerivedUnion, 20);
		data.top = new FormAttachment(0, 0);
		isOrdered.setLayoutData(data);

		isOrdered.addSelectionListener(isOrderedListener = new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				((Property)getElement()).setIsOrdered(isOrdered.getSelection());
			}

			public void widgetDefaultSelected(SelectionEvent e){
			}
		});


		isReadOnly = getWidgetFactory().createButton(composite, "isReadOnly", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(isOrdered, 20);
		data.top = new FormAttachment(0, 0);
		isReadOnly.setLayoutData(data);

		isReadOnly.addSelectionListener(isReadOnlyListener = new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				((Property)getElement()).setIsReadOnly(isReadOnly.getSelection());
			}

			public void widgetDefaultSelected(SelectionEvent e){
			}
		});


		isStatic = getWidgetFactory().createButton(composite, "isStatic", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(isReadOnly, 20);
		data.top = new FormAttachment(0, 0);
		isStatic.setLayoutData(data);

		isStatic.addSelectionListener(isStaticListener = new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				((Property)getElement()).setIsStatic(isStatic.getSelection());
			}

			public void widgetDefaultSelected(SelectionEvent e){
			}
		});

		isUnique = getWidgetFactory().createButton(composite, "isUnique", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(isStatic, 20);
		data.top = new FormAttachment(0, 0);
		isUnique.setLayoutData(data);

		isUnique.addSelectionListener(isUniqueListener = new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				((Property)getElement()).setIsUnique(isUnique.getSelection());
			}

			public void widgetDefaultSelected(SelectionEvent e){
			}
		});


		CLabel derivedLabel = getWidgetFactory().createCLabel(composite, "Qualifiers:"); //$NON-NLS-1$
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(isDerived,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(isDerived, 0, SWT.CENTER);
		derivedLabel.setLayoutData(data);

	}

	/**
	 * 
	 */
	@Override
	public void refresh() {
		if( !isDerived.isDisposed() &&
				!isDerivedUnion.isDisposed()&&
				!isReadOnly.isDisposed() &&
				!isStatic.isDisposed()&&
				!isUnique.isDisposed()){
			if(getElement() instanceof Property){

				isDerived.setSelection(((Property)getElement()).isDerived());
				isDerivedUnion.setSelection(((Property)getElement()).isDerivedUnion());
				isOrdered.setSelection(((Property)getElement()).isOrdered());
				isReadOnly.setSelection(((Property)getElement()).isReadOnly());
				isStatic.setSelection(((Property)getElement()).isStatic());
				isUnique.setSelection(((Property)getElement()).isUnique());
			}
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(isDerived!=null && !isDerived.isDisposed())
			isDerived.removeSelectionListener(isDerivedListener);
		if(isDerivedUnion!=null && !isDerivedUnion.isDisposed())
			isDerivedUnion.removeSelectionListener(isDerivedUnionListener);
		if(isOrdered!=null && !isOrdered.isDisposed())
			isOrdered.removeSelectionListener(isOrderedListener);
		if(isReadOnly!=null && !isReadOnly.isDisposed())
			isReadOnly.removeSelectionListener(isReadOnlyListener);
		if(isStatic!=null && !isStatic.isDisposed())
			isStatic.removeSelectionListener(isStaticListener);
		if(isUnique!=null && !isUnique.isDisposed())
			isUnique.removeSelectionListener(isUniqueListener);
	}
}

